/**
 *
 * Actions
 * 
 */

Ext.ns('app'); 

app.Actions = function(){
	
	/*
	 * Private functions
	 */
	
	function doCreateTask(){
		var projects = new Ext.data.Store({
			
			reader: new Ext.data.JsonReader({
				totalProperty: 'results',
				id: 'id', 
				fields: ['id', 'name', 'description']
			})
		
			, proxy: new Ext.data.DirectProxy({
				directFn: TaskWall.getProjects
			}) 
		
//			, baseParams: {
//				fields: ['id', 'name', 'description']
//			} 
		
			, sortInfo: {
				field: 'name',
				direction: 'ASC'
			}
		
		}); 
		
		projects.load(); 
		
		var startField = new Ext.form.Checkbox({name:'start',fieldLabel:'Start now'});
		var showField = new Ext.form.Checkbox({fieldLabel:'Show in Task Wall'}); 
		var showHidden = new Ext.form.Hidden({name:'show', value:0}); 
		
		startField.on('check',function(field,checked){
			showField.setValue(checked); 
			showField.setDisabled(checked);
		}); 
		
		showField.on('check',function(field,checked){
			showHidden.setValue(checked ? 1 : 0); 
		}); 
		
		var formWin = new app.widgets.FormWin({
			title: 'New Task' 
			, height: 260 
			, formItems:[
				{name:'name',fieldLabel:'Task Name',allowBlank:false}
				,{
					name: 'project', 
					fieldLabel: 'Project', 
					xtype:'combo', 
					store: projects, 
					valueField: 'id', 
					displayField: 'name',
					hiddenName:'project',
					mode: 'local',
					triggerAction: 'all', 
					allowBlank:false
				}
				,{name: 'description',fieldLabel: 'Description',xtype: 'textarea'}
				,startField
				,showField
				,showHidden
			]
			, formApi : {
				submit: TimeTracker.createTask
			} 
			, onSuccess : function(form, action){	
				
				//checks whether the flag 'start now' was on
				if(form.findField('start').getValue()) {
					//if so, set current task as the running task 
					app.getTaskWall().setCurrentTask(action.result.taskId); 
				}
				
				
				var msg = 'Form successfully submitted.'; 
				
				if(action.result){
					msg = action.result.msg ? action.result.msg : msg;  
				}
				
				app.Msg.info(msg, 'Success');  
				this.close(); 
			}
		}); 
		
		//before submit 
		formWin.getFormPanel().getForm().on('beforeaction',function(form,action){

			if(action.type == "directsubmit" || action.type == "submit"){

				//checks whether the flag 'start now' is on
				if(form.findField('start').getValue()) {
					//if so, stops current task 
					app.getTaskWall().stopTask(); 
				}
			}
			
		}); 
		
		formWin.show(); 
	}
	
	function doCreateProject(){
		new app.widgets.FormWin({
			title: 'New Project' 
			, height: 200 
			, formItems:[
			           {name:'name', fieldLabel:'Project Name', allowBlank:false}
			           ,{name:'description', fieldLabel:'Description', xtype:'textarea'}
			           ,{name:'start', fieldLabel:'Start now', xtype:'checkbox'}
			           ] 
			, formApi : {
				submit: TimeTracker.createProject
			}
		}).show(); 
	}
	
	
	return {
		
		createTask : function(){
			
			if (!this.createTaskAction){
				this.createTaskAction = new Ext.Action({
				    text: 'New Task',
				    handler: doCreateTask,
				    iconCls: 'add-icon'
				});
			}
			
			return this.createTaskAction;
		}
	
		, createProject : function(){
			
			if (!this.createProjectAction){
				this.createProjectAction = new Ext.Action({
				    text: 'New Project',
				    handler: doCreateProject,
				    iconCls: 'add-icon'
				});
			}
			
			return this.createProjectAction; 
		}
	
	}; 
}(); 